#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

    initSignalAndSlots();

    ui->tabWidget->setCurrentIndex(0);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initSignalAndSlots()
{
    //新建文件
    connect(ui->action_newFile, &QAction::triggered, this, &MainWindow::newFile);

    //打开文件
    connect(ui->action_openFile, &QAction::triggered, this, &MainWindow::openFile);

    //保存文件
    connect(ui->action_saveFile, &QAction::triggered, this, &MainWindow::saveFile);

    //复制内容
    connect(ui->action_copy, &QAction::triggered, this, &MainWindow::copyToClipboard);

    //粘贴内容
    connect(ui->action_paste, &QAction::triggered, this, &MainWindow::pastToTextEdit);

    //关闭当前tab页
    connect(ui->action_closeCurrent, &QAction::triggered, this, &MainWindow::closeCurrentTab);

    //关闭所有tab页
    connect(ui->action_closeAll, &QAction::triggered, this, &MainWindow::closeAllTab);

}

QTextEdit *MainWindow::getCurrentTextEdit()
{
    int tabIndex = ui->tabWidget->currentIndex();

    qDebug() << "tabIndex=" << tabIndex;

    QWidget * currWidget = ui->tabWidget->widget(tabIndex);

    QTextEdit *textEdit = Q_NULLPTR;

    QList<QTextEdit *> allTextEditList = currWidget->findChildren<QTextEdit *>();
    for(QTextEdit * item : allTextEditList){
        QString objectName = item->objectName();
        if(objectName.startsWith("textEdit")){
            textEdit = item;

            qDebug() << "============";
            break;
        }
    }
    return textEdit;
}

QString MainWindow::getCurrentTabObjectName()
{
    int tabIndex = ui->tabWidget->currentIndex();

    QWidget * currWidget = ui->tabWidget->widget(tabIndex);
    QString tabObjectName = currWidget->objectName();
    return tabObjectName;
}

void MainWindow::newFile()
{
    qDebug() << "new file";

    int tabCount = ui->tabWidget->count();
    qDebug() << "当前有" << tabCount << "个tab";

    QString newTabTitle = QString("新建%1").arg(QString::number(tabCount + 1));

    int rand = qrand();

    QString tabName = "tab_" + QString::number(rand);

    QWidget * continerWidget = new QWidget;
    continerWidget->setFixedSize(ui->tabWidget->width(), ui->tabWidget->height());
    continerWidget->setObjectName(tabName);

    tabFileMap[tabName] = "";

    QTextEdit *textEdit = new QTextEdit;
    textEdit->setFixedSize(continerWidget->width(), continerWidget->height());
    textEdit->setObjectName("textEdit_" + QString::number(tabCount + 1));
    textEdit->setParent(continerWidget);

    ui->tabWidget->addTab(continerWidget, newTabTitle);
    ui->tabWidget->setCurrentIndex(tabCount);

}

void MainWindow::openFile()
{
    qDebug() << "open file";

    QString homePath = QDir::homePath();
    QString dialogTitle = "选择文件";
    QString fileTypeFilter = "TXT文件(*.txt);;LOG文件(*.log);;全部(*.*)";

    QString filePath = QFileDialog::getOpenFileName(this, dialogTitle, homePath, fileTypeFilter);

    QFile file(filePath);
    if(!file.open(QIODevice::ReadWrite)){
        QMessageBox::critical(this, "错误", "请选择文件!");
        return;
    }

    QString tabObjectName = this->getCurrentTabObjectName();

    this->tabFileMap[tabObjectName] = filePath;

    QTextStream textStreamRead(&file);

    QString fileDataString = textStreamRead.readAll();

    QTextEdit *textEdit = getCurrentTextEdit();

    textEdit->setText(fileDataString);

    textStreamRead.flush();

    file.flush();
    file.close();
}



void MainWindow::saveFile()
{

    QString tabObjectName = this->getCurrentTabObjectName();

    QString filePath = this->tabFileMap[tabObjectName];
    if("" == filePath){
        QString homePath = QDir::homePath();
        filePath = QFileDialog::getSaveFileName(this, tr("Save File"), homePath, tr("文本文件(*.txt)"));
        qDebug() << "要保存的文件路径:" << filePath;
    }


    QFile file(filePath);

    if(!file.open(QIODevice::ReadWrite)){
        QMessageBox::critical(this, "错误", "保存文件失败!");
        return;
    }

    QTextEdit *textEdit = getCurrentTextEdit();

    QString textContent = textEdit->toPlainText();

    QTextStream textStreamWrite(&file);

    textStreamWrite << textContent;
    textStreamWrite.flush();

    file.flush();
    file.close();

}

void MainWindow::copyToClipboard()
{
    QTextEdit *textEdit = getCurrentTextEdit();
    QString textContent = textEdit->toPlainText();

    this->clipboard = QApplication::clipboard();
    clipboard->setText(textContent);

    QString content = clipboard->text();
    qDebug() << content;
}

void MainWindow::pastToTextEdit()
{
    QString content = this->clipboard->text();
    qDebug() << content;

    QTextEdit *textEdit = getCurrentTextEdit();
    textEdit->setText(content);
}

void MainWindow::closeCurrentTab()
{
    int currentIndex = ui->tabWidget->currentIndex();
    int count = ui->tabWidget->count();
    if(count == 1){
        return;
    }

    ui->tabWidget->removeTab(currentIndex);
}

void MainWindow::closeAllTab()
{
    ui->tabWidget->clear();
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->removeTab(index);
}



























































































